#ifndef __MWC_GENERATOR_HPP__
#define __MWC_GENERATOR_HPP__

namespace utils {

	// Implementación del generador MWC inventado por George Marsaglia.
	template <typename T = unsigned long, size_t A=36969, size_t B=18000, size_t MASK = 65535, size_t STEP = 16>
	class mwc_generator {
		public:
			typedef unsigned long seed_t;

		protected:
			seed_t w,z;

		public:
			mwc_generator (seed_t z, seed_t w) {
				this->z = z;
				this->w = w;
			}

			mwc_generator (const T& z, const T& w) {
				this->z = reinterpret_cast<const seed_t&> (z);
				this->w = reinterpret_cast<const seed_t&> (w);
			}

			void next() {
				this->z = A * (this->z & MASK) + (this->z >> STEP);
				this->w = B * (this->w & MASK) + (this->w >> STEP);
			}

			T operator() () {
				next();
				return actual();
			}

			T actual() const {
				return (this->z << STEP) + this->w;
			}

			virtual ~mwc_generator () {};
	}; // mwc_generator

} // namespace utils

#endif /* __MWC_GENERATOR_HPP__ */

